/**
 * Class for dealing with tile+unit descriptions, whether they be in the toolbar or a tooltip
 */
TileInfo = new Class({
	
	_type : "TileInfo",
	
	Implements : Options,
	
	/**
	 * @var Element wrapper for info
	 */
	container : null,
	
	options : {
		'infName'	: '',
		'infClass'	: '',
		'infCoords'	: '',
		'infBearing': ''
	},
	
	/**
	 * TileInfo Constructor
	 */
	initialize : function(container,options){
		this.container = container;
		this.container.setStyle('opacity',0);
		this.setOptions(options);
	},
	
	/**
	 * Hide container
	 */
	hide : function(){
		this.container.fade('out');
	},
	
	/**
	 * Show container
	 */
	show : function(tileInfo){
		if(tileInfo){
			g = GameEng.getInstance();
			this.bindObjectToOptions(tileInfo);
			if(tileInfo._type=="Unit"){
				if(tileInfo.getPlayer()!=g.hostPlayer){
					this.container.addClass("enemy");
				}else{
					this.container.removeClass("enemy");
				}
			}
		}
		this.bindInfoToPage();
		this.container.fade('in');
	},
	
	/**
	 * Take a unit/tile info object and bind to this.options
	 * @param Unit|other? info
	 */
	bindObjectToOptions : function(info){
		switch(info._type){
			case "Unit":
				var coords = info.getCoordinates();
				this.setOptions({
					'infName' 	: info.id,
					'infClass'	: info.getMovementTypeDesc(),
					'infCoords'	: '['+coords.x+','+coords.y+']',
					'infBearing': info.getBearingDesc()
				});
				break;
				
			default:
				throw new Error("Unknown tile/unit type for tile info panel");
		}
	},
	
	/**
	 * Take options property and bind to container
	 */
	bindInfoToPage : function(){
		obj = this;
		$each(this.options,function(dfn,label){
			wrpr = obj.container.getChildren('.'+label);
			if(wrpr){
				for(var n=0;n<wrpr.length;n++){
					wrpr[n].getElement('dfn').set('text',dfn);
				}
			}
		});
	},
	
	/**
	 * Update panel with latest info
	 * @param Object info
	 */
	update : function(info){
		this.bindObjectToOptions(info);
		this.bindInfoToPage();
	}
})